package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.model.User;
import com.c611.smartclassroom.model.WorkOrder;
import com.c611.smartclassroom.service.WorkOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;



/*
* 工单管理：/workOrder	（李源）
	1，查询所有工单，按时间排序，并分页	queryAllWorkOrder(当前页，每页显示条数)
	2，按工单编号进行查询	queryByWorkOrderId(工单编号)
	3，填写处理结果			saveWorkOrderResult(处理结果)
	4，查询该工单的处理结果	queryWorkOrderResult(工单编号)
	-5，删除工单		delWorkOrder(工单编号)
*
*工单管理：t_workOrder
	工单编号：wOrderID varchar(20) 非空 关键字 自增长
	报修人：applicant varchar(20) 非空
	报修学校ID：schoolID varchar(20) 非空
    教学楼ID：tBuildID varchar(10) 非空
    教室名称ID：classroomID varchar(10) 非空
	联系电话：userTel varchar(11) 非空
	报修时间：repairTime smalldatetime 年月日时分秒（自动填入）
	报修问题：reProblem varchar(128) 非空
	状态：workStatus int //0:未处理 1:处理中 2:已处理 非空
	处理结果：hResult varchar(128) 允许为空
	置废标识：delFlag int 默认为0
*
* */



@RestController
@RequestMapping("/workOrder")
public class WorkOrderController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    WorkOrderService workOrderService;

    /**
     * 查询所有工单 按时间排序，并分页
     * @param page 当前页
     * @param pageSize  每页的显示条数
     * @return
     */
    @PostMapping("/queryWorkOrder")
    public DataGridView<WorkOrder> queryWorkOrder(@Param("page") Integer page,@Param("pageSize") Integer pageSize){
        //1.查询页数是否合理
        if (page < 1 || pageSize < 1)
            return null;
        //2.查询数据库中的工单表中的工单总数
        long workOrderCount = workOrderService.queryWorkOrderCount();
        //3.计算该页是否为空（前一页的记录数都大于数据库总数）
        if ((page - 1) * pageSize >= workOrderCount)
            return null;
        //4.参数合理，调用分页查询的方法
        return new DataGridView<WorkOrder>(0,"ok",workOrderCount,workOrderService.queryWorkOrder(page,pageSize));
    }

    /**
     * 根据名字来进行模糊分页查询
     */
    @PostMapping("/queryWorkOrderByName")
    public DataGridView<WorkOrder> queryWorkOrder(@Param("page") Integer page,@Param("pageSize") Integer pageSize,@Param("name") String name) {
        //1.查询页数是否合理
        if (page < 1 || pageSize < 1)
            return null;
        //2.查询数据库中的工单表中的工单总数
        long workOrderCount = workOrderService.queryWorkOrderCountByName(name);
        //3.计算该页是否为空（前一页的记录数都大于数据库总数）
        if ((page - 1) * pageSize >= workOrderCount)
            return null;
        //4.参数合理，调用分页查询的方法
        return new DataGridView<WorkOrder>(0,"ok",workOrderCount,workOrderService.queryWorkOrderByName(page,pageSize,name));
    }

        /**
         * 根据工单Id查询工单
         * @param worderid
         * @return
         */
    @PostMapping("/queryByWorkOrderId")
    public List<WorkOrder> queryByWorkOrderId(@Param("worderid") Integer worderid){

        if (worderid == null) return null;
        //查询数据库中的工单表中的工单总数
        long workOrderCount = workOrderService.queryWorkOrderCount();
        //如果输入编号大于工单总数，返回空
        if (worderid > workOrderCount) return null;
        return workOrderService.queryByWorkOrderId(worderid);
    }

    /**
     * 添加工单
     * @param workOrder
     * @return
     */
    @PostMapping("/addWorkOrder")
    public int addWorkOrder(@Param("workOrder") WorkOrder workOrder){

        //获取字段，进行判断
        String applicant = workOrder.getApplicant();
        String schoolID = workOrder.getSchoolid();
        String tBuildID = workOrder.getTbuildid();
        String classroomID = workOrder.getClassroomid();
        String userTel = workOrder.getUsertel();
        String reProblem = workOrder.getReproblem();


        if (workOrder == null) return 0;
        //输入的以下字段为空则返回 0
        if (applicant == null || schoolID == null || tBuildID == null
                || classroomID == null || reProblem == null)
            return 0;

        //判断手机号码是否为11位数字
        if (userTel.length() != 11)
            return 0;
        return workOrderService.addWorkOrder(workOrder);
    }

    /**
     * 增加工单，根据当前人的信息
     */
    @PostMapping("/addWorkOrder2")
    public int addWorkOrder2(@RequestParam("schoolId") String schoolId,
                             @RequestParam("buildId") String buildId,
                             @RequestParam("classroomId") String classroomId,
                             @RequestParam("desc") String desc,
                             HttpSession session){
        User user = (User)session.getAttribute("loginUser");
        if(user == null || schoolId == null || buildId == null || classroomId == null
        || "".equals(schoolId) || "".equals(buildId) || "".equals(classroomId)) {
            return 0;
        }

        //封装订单对象
        WorkOrder workOrder = new WorkOrder();
        workOrder.setApplicant(user.getUserrealname());
        workOrder.setSchoolid(schoolId);
        workOrder.setTbuildid(buildId);
        workOrder.setClassroomid(classroomId);
        workOrder.setUsertel(user.getUsertel());
        workOrder.setReproblem(desc);
        return workOrderService.addWorkOrder(workOrder);
    }

    /**
     * 删除工单
     * @param wOrderID
     * @return
     */
    @PostMapping("/delWorkOrder")
    public int delWorkOrder(@Param("wOrderID") Integer wOrderID){
        if (wOrderID == null)
            return 0;
        return workOrderService.delWorkOrder(wOrderID);
    }

    /**
     * 填写处理结果
     * @param wOrderID
     * @param hResult
     * @return
     */
    @PostMapping("/saveWorkOrderResult")
    public int saveWorkOrderResult(@Param("wOrderID") int wOrderID , @Param("hResult") String hResult){
        if (wOrderID == 0)
            return 0;
        return workOrderService.saveWorkOrderResult(wOrderID,hResult);
    }

    /**
     * 查询该工单的处理结果
     * @param worderid
     * @return
     */
    @PostMapping("/queryWorkOrderResult")
    public String queryWorkOrderResult(@Param("worderid") Integer worderid){
        if (worderid == null) return null;

        return workOrderService.queryWorkOrderResult(worderid);
    }

}
